using UnityEngine;
using System.Collections.Generic;

public class BoardScript : MonoBehaviour
{
	public List<GameObject> m_lHoles = new List<GameObject>();
	List <GameObject> m_lWalls = new List<GameObject>();
	List<GameObject> m_lPowerUps = new List<GameObject>();
	GameObject m_MainBall;
	float m_fSpeed = 14.5f;

	// Use this for initialization
	void Start()
	{
		foreach (GameObject j in GameObject.FindGameObjectsWithTag("Hole"))
		{
			m_lHoles.Add(j);
		}
		
		
		foreach (GameObject i in GameObject.FindGameObjectsWithTag("Wall"))
		{
		
			m_lWalls.Add(i);
		}
		m_MainBall = FindMainBall();
		

	}
	GameObject FindMainBall()
	{
		List<GameObject> _Balls = new List<GameObject>();
			
		foreach (GameObject g in GameObject.FindGameObjectsWithTag("Ball"))
		{
			if(g.GetComponent<BallController>().IsMain)
			{
				m_MainBall = g;
				return g;
			}
			else
				_Balls.Add(g);
		}
		int i = Random.Range(0, _Balls.Count);
		_Balls[i].GetComponent<BallController>().IsMain = true;
		return _Balls[i];
	}

	// Update is called once per frame
	void Update() 
	{
		if (Input.GetButton("Fire1"))
		{
			transform.rotation = Quaternion.Euler(Input.GetAxis("Vertical") * m_fSpeed, 0.0f, Input.GetAxis("Horizontal") * m_fSpeed);	
		}
		if (GameObject.Find("Ball"))
		{
			for (int i = 0; i < m_lHoles.Count; ++i)
			{
				foreach (GameObject ball in GameObject.FindGameObjectsWithTag("Ball"))
				{
					print("Checking the holes");
					float dist = Vector3.Distance(ball.transform.position, m_lHoles[i].transform.position);
					//print(dist);
					if (dist <= m_lHoles[i].GetComponent<HoleController>().Radius)
					{

						GameObject.Find("GUIScoreObj").SendMessage("IncreaseScore", m_lHoles[i].GetComponent<HoleController>().Score);
						Destroy(ball);
						m_lHoles[i].SendMessage("DestroyHole");
						m_lHoles.RemoveAt(i);
						
						//Destroy(m_lHoles[i]);
						print("Next Level");
					}
				}
			}
			for (int j = 0; j < m_lWalls.Count; ++j)
			{
				if(Vector3.Distance(m_MainBall.transform.position, m_lWalls[j].transform.position) < (m_MainBall.GetComponent<SphereCollider>().radius + m_lWalls[j].GetComponent<WallController>().Radius))
				{
					float tempforce = m_lWalls[j].GetComponent<WallController>().Force;
					Vector3 VecTo = m_MainBall.transform.position - m_lWalls[j].transform.position;
					if(m_MainBall.GetComponent<BallController>().m_ePolarity == ePole.e_Negative)
					{
						if(m_lWalls[j].GetComponent<WallController>().m_ePolarity == ePole.e_Positive)
						{
							m_MainBall.GetComponent<Rigidbody>().AddForce(VecTo *tempforce, ForceMode.Acceleration);
						}
						else if(m_lWalls[j].GetComponent<WallController>().m_ePolarity == ePole.e_Negative)
						{
							m_MainBall.GetComponent<Rigidbody>().AddForce(VecTo * -tempforce, ForceMode.Acceleration);
						}
					}
					else if(m_MainBall.GetComponent<BallController>().m_ePolarity == ePole.e_Positive)
					{	
						if(m_lWalls[j].GetComponent<WallController>().m_ePolarity == ePole.e_Positive)
						{
							m_MainBall.GetComponent<Rigidbody>().AddForce(VecTo * -tempforce, ForceMode.Acceleration);
							
						}
						else if(m_lWalls[j].GetComponent<WallController>().m_ePolarity == ePole.e_Negative)
						{
							m_MainBall.GetComponent<Rigidbody>().AddForce(VecTo * tempforce, ForceMode.Acceleration);
							
						}
					}
				}
			}
		}
		else
		{
			print("gameOver");
			print("no ball");
		}
		}

}
